package respacker

import (
	"log"
	"os"
	"path/filepath"
	"strings"

	"gitee.com/StarCitizen_CN/stargox/pkg/logger"
)

var (
	srcMap               = make(map[string]*L10nFileInfo)
	srcDirDataMap        = make(map[string]*L10nFileInfo)
	srcDirDatAllTextMap  = make(map[string]*L10nFileInfo)
	srcDirDatPartTextMap = make(map[string]*L10nFileInfo)

	srcFileGlobalIniAllMap  = make(map[string]*L10nFileInfo)
	srcFileGlobalIniPartMap = make(map[string]*L10nFileInfo)
	srcFileDevDllMap        = make(map[string]*L10nFileInfo)
	srcDirDDSMap            = make(map[string]*L10nFileInfo)
)

func split() {
	for relPath, fi := range srcMap {

		_, fileName := filepath.Split(relPath)
		fileNameLow := strings.ToLower(fileName)

		isDDS := strings.Contains(fileNameLow, ".dds")
		isDLL := fi.Name == SRC_FILE_DEV_DLL

		isGlobalCfgAll := fileNameLow == SRC_FILE_INI_ALL
		isGlobalCfgPart := fileNameLow == SRC_FILE_INI_PART

		isDataContent := strings.HasPrefix(relPath, "data\\")

		if isDDS {
			delete(srcMap, relPath)
			srcDirDDSMap[relPath] = fi
			continue
		}

		if isDLL {
			delete(srcMap, relPath)
			fi.RelPath = OUTPUT_REL_FILE_DEV_DLL
			srcFileDevDllMap[OUTPUT_REL_FILE_DEV_DLL] = fi
			continue
		}

		if isGlobalCfgAll || isGlobalCfgPart {
			delete(srcMap, relPath)
			nonBoxVersionFi := fi.Copy()

			fi.RelPath = fileNameLow

			nonBoxVersionFi.RelPath = OUTPUT_REL_FILE_INI
			nonBoxVersionFi.Name = "global.ini"

			if isGlobalCfgAll {
				srcFileGlobalIniAllMap[fileNameLow] = fi
				srcDirDatAllTextMap[nonBoxVersionFi.Name] = nonBoxVersionFi
			}

			if isGlobalCfgPart {
				srcFileGlobalIniPartMap[fileNameLow] = fi
				srcDirDatPartTextMap[nonBoxVersionFi.Name] = nonBoxVersionFi
			}

			continue
		}

		if isDataContent {
			delete(srcMap, relPath)
			srcDirDataMap[relPath] = fi
			continue
		}

	}

	if len(srcMap) != 0 {
		log.Println(len(srcMap))
		log.Println(srcMap)
		log.Fatalln("出了点事")
	}
}

func Go() {

	if err := logger.Setup(
		logger.SetOutput(OUTPUT_PATH+"\\log\\respacker", false),
		logger.SetDebugMode(true),
	); err != nil {
		logger.ShowGuiMsg("初始化日志工具异常", err.Error())
		os.Exit(1)
	}

	var scanErr error
	srcMap, scanErr = Scan(SRC_PATH)
	if scanErr != nil {
		log.Fatalln(scanErr)
	}

	split()

	Write(OUTPUT_ABS_DIR_STARBOX_VERSION, []map[string]*L10nFileInfo{srcFileGlobalIniAllMap, srcFileGlobalIniPartMap, srcDirDataMap, srcFileDevDllMap})
	Write(OUTPUT_ABS_DIR_STARBOX_VERSION+"\\dds", []map[string]*L10nFileInfo{srcDirDDSMap})

	Write(OUTPUT_ABS_DIR_MANUAL_VERSION+"\\半汉化基本版", []map[string]*L10nFileInfo{srcDirDatPartTextMap, srcFileDevDllMap, srcDirDataMap})
	Write(OUTPUT_ABS_DIR_MANUAL_VERSION+"\\全汉化基本版", []map[string]*L10nFileInfo{srcDirDatAllTextMap, srcFileDevDllMap, srcDirDataMap})
	Write(OUTPUT_ABS_DIR_MANUAL_VERSION+"\\半汉化完全版", []map[string]*L10nFileInfo{srcDirDatPartTextMap, srcFileDevDllMap, srcDirDataMap, srcDirDDSMap})
	Write(OUTPUT_ABS_DIR_MANUAL_VERSION+"\\全汉化完全版", []map[string]*L10nFileInfo{srcDirDatAllTextMap, srcFileDevDllMap, srcDirDataMap, srcDirDDSMap})

}
